package com.mop.dinnerbook.dao;

import com.mop.dinnerbook.bean.Action;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
/*
 用户操作日志类
 */
public class ActionDAO extends BaseDAO{
    /*
     记录用户的订餐和取消订餐的日志
     */
	public boolean saveAction(Action action){
                DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                Date now = Calendar.getInstance().getTime();
                String nows = formatter.format(now);
		String isql = "insert into action(aid,aname,actionIp,mid,adate) values(NULL,'"+action.getAname()+"','"+action.getActionIp()+"',"+action.getMember().getMid()+",'"+ nows +"')";
		boolean result = false;
		try {
			result = executeInsert(isql) > 0;
			return result;
		} catch (Exception e) {
                        System.out.println(isql);
			e.printStackTrace();
		} 
		return false;
	}
        /*
         根据IP获取当天的被操作者的列表信息
         */
        public List getOperator(String actionIp){
            List result = null;
            String sql="select a.* from(select a.aid,c.pname,b.mname,a.actionIp,count(1) as total from action a,member b,project c where a.actionIp='"+actionIp+"' and a.adate =curdate() and b.mid = a.mid and c.pid= b.pid group by b.mid having total >= 1) a order by a.total desc";
            try {
                            result = executeSelectList(sql, new Processor(){
                                    public Object product(ResultSet rst) throws SQLException{
                                            Action action = new Action();
                                            action.setAid(rst.getInt(1));
                                            action.setPname(rst.getString(2));
                                            action.setAname(rst.getString(3));
                                            action.setActionIp(rst.getString(4));
                                            action.setTotal(rst.getInt(5));
                                            return action;
                                    }
                            });
                    } catch (SQLException e) {
                            System.out.println(sql);
                            e.printStackTrace();
                    }
            return result;
        }
}
